package com.yunhe.rentcars.pro.controller.system;

import com.yunhe.rentcars.commons.dto.JSONResponse;
import com.yunhe.rentcars.pro.entity.permi.User;
import com.yunhe.rentcars.pro.service.permi.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2017/11/29 0029.
 */
@Controller
@RequestMapping("/accountmsg")
public class AccountMsgController {

    @Autowired
    private UserService userService;

    @RequestMapping
    public String accountMsg(HttpServletRequest req){

        User user = (User)req.getSession().getAttribute("user");

        req.setAttribute("user",user);
        return "system/accountmsg/accountmsg";
    }


    @RequestMapping("/updateMsg")
    @ResponseBody
    public JSONResponse update(User user){
        userService.update(user);
        return JSONResponse.success("账户信息修改成功");
    }


    @RequestMapping("/resetPwd")
    @ResponseBody
    public JSONResponse mmpd(HttpServletRequest req, String pwd, String newPwd, String newPwd2){
        User user = (User) req.getSession().getAttribute("user");
        String message = "";
        if(pwd.equals(user.getPassword())){
            if(newPwd.equals(newPwd2)){
                if(!newPwd.equals(user.getPassword())){

                    User user1 = new User();
                    user1.setPassword(newPwd);
                    user1.setUid(user.getUid());
                    userService.update(user);
                    return JSONResponse.success("密码修改成功");
                }else{
                    message = "新密码与原密码相同";
                }
            }else{
                message = "两次密码不一致";
            }
        }else{
            message = "用户密码输入错误";
        }
        return JSONResponse.fail(message);

    }
}
